package com.wtgroup.demo.common.util;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@NoArgsConstructor
public class ABTest {
    String a;
    String b;
    List<Long> aTimes = new ArrayList<>();
    List<Long> bTimes = new ArrayList<>();

    public ABTest(String a, String b) {
        this.a = a;
        this.b = b;
    }

    public void runA(Runnable a) {
        aTimes.add(Tools.time(a));
    }

    public void runB(Runnable b) {
        bTimes.add(Tools.time(b));
    }

    public void print() {
        Long at = aTimes.stream().collect(Collectors.summingLong(e -> e));
        Long bt = bTimes.stream().collect(Collectors.summingLong(e -> e));
        String str = StrUtil.format("{}            {}            -\n" +
                        DateUtil.formatBetween(at) + "  " + DateUtil.formatBetween(bt) + "  " + (at - bt),
                a == null ? "A" : a, b == null ? "B" : b);
        str += "\n---\n" +
                "iter: " + aTimes.size();
        System.out.println(str);
    }
}